package com.olddriver.crm.service.impl;

import com.olddriver.crm.domain.*;
import com.olddriver.crm.mapper.AccidentBillItemMapper;
import com.olddriver.crm.mapper.CompensationMapper;
import com.olddriver.crm.mapper.PaidListMapper;
import com.olddriver.crm.mapper.TotalInfoMapper;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.QueryObject;
import com.olddriver.crm.service.IAccidentBillItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class AccidentBillItemServiceImpl implements IAccidentBillItemService{

    @Autowired
    private AccidentBillItemMapper accidentBillItemMapper;
    @Autowired
    private TotalInfoMapper totalInfoMapper;
    @Autowired
    private CompensationMapper compensationMapper;
    @Autowired
    private PaidListMapper paidListMapper;

    @Override
    public int insert(AccidentBillItem accidentBillItem){
        return accidentBillItemMapper.insert(accidentBillItem);
    }


    @Override
    public List<AccidentBillItem> listAll() {
        return accidentBillItemMapper.selectAll();
    }

    @Override
    public AccidentBillItem selectByPrimaryKey(Long id) {
        return accidentBillItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult query(QueryObject qo) {
        int total = accidentBillItemMapper.queryTotal(qo);
        if (total == 0) {
            return new PageResult(total, Collections.emptyList());
        }
        return new PageResult(total, accidentBillItemMapper.queryRows(qo));
    }

    //编辑事故资料
    @Override
    public void updateCaseState(AccidentBillItem accident) {

        accidentBillItemMapper.updateCaseState(accident);
    }
    //上报审批
    @Override
    public void changeAppearState(Long id)
    {
        accidentBillItemMapper.changeAppearState(id);
    }

    //审核生成一张处理事故明细表
    @Override
    public void changeAuditState(Long id){
        AccidentBillItem accident = accidentBillItemMapper.selectByPrimaryKey(id);
        String policySn = accident.getPolicySn();
        Compensation compensation = compensationMapper.getCompensationByAccId(id);
        PaidList paidList = paidListMapper.getPaidListBySn(policySn);
        Claims claim = accident.getClaim();
        TotalInfo info = new TotalInfo();
        info.setState(2);//已理赔
        info.setReporter(claim.getReporterName());
        info.setReportTime(claim.getAccidentTime());
        info.setAccidentPlace(claim.getAccidentAddress());
        info.setEmployee(accident.getHandler().getUsername());
        //车主
        info.setOwner(compensation.getReceiverName());
        //车型
        String carType = paidList.getInsuranceList().getCarInformation().getCarType();
        info.setOrganization(paidList.getInsuranceList().getCarInformation().getCarType());
        //保单号
        info.setPolicySn(compensation.getSn());
        info.setPolicySn(accident.getPolicySn());
        //保险机构
        String name = paidList.getInsuranceList().getProductItem().getName();
        info.setOrganization(paidList.getInsuranceList().getProductItem().getName());
        //保险类型
        info.setCaseContent(accident.getAccidentDescribe());
        info.setDutyContent(accident.getAccidentDutyAssign());
        totalInfoMapper.insert(info);
        accidentBillItemMapper.delete(id);
    }

    @Override
    public AccidentBillItem getClaimByClamId(Long id) {
        return accidentBillItemMapper.getClaimByClamId(id);
    }

    @Override
    public int getUndealAccident() {
        return accidentBillItemMapper.getUndealAccident();
    }

    //拒保
    @Override
    public void declinature(Long id) {
        AccidentBillItem accident = accidentBillItemMapper.selectByPrimaryKey(id);
        String policySn = accident.getPolicySn();
        Compensation compensation = compensationMapper.getCompensationByAccId(id);
        PaidList paidList = paidListMapper.getPaidListBySn(policySn);
        Claims claim = accident.getClaim();
        TotalInfo info = new TotalInfo();
        info.setState(1);//不受理
        info.setReporter(claim.getReporterName());
        info.setReportTime(claim.getAccidentTime());
        info.setAccidentPlace(claim.getAccidentAddress());
        info.setEmployee(accident.getHandler().getUsername());
        //车主
        info.setOwner(compensation.getReceiverName());
        //车型
        String carType = paidList.getInsuranceList().getCarInformation().getCarType();
        info.setOrganization(paidList.getInsuranceList().getCarInformation().getCarType());
        //保单号
        info.setPolicySn(compensation.getSn());
        info.setPolicySn(accident.getPolicySn());
        //保险机构
        String name = paidList.getInsuranceList().getProductItem().getName();
        info.setOrganization(paidList.getInsuranceList().getProductItem().getName());
        //保险类型
        info.setCaseContent(accident.getAccidentDescribe());
        info.setDutyContent(accident.getAccidentDutyAssign());
        totalInfoMapper.insert(info);
        accidentBillItemMapper.delete(id);
    }

    @Override
    public void delete(Long id) {
        accidentBillItemMapper.delete(id);
    }

}
